using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Topics : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            divAttachments.Visible = false;
        }
    }

    protected void dsTopics_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Topic> e)
    {
        e.SetData(Acms.Bll.Topic.GetAllBySiteId(AcmsSite.Id));

    }

    protected void dsTopics_UpdatingData(object sender, Acms.DataControls.UpdatingDataEventArgs<Acms.Bll.Topic> e)
    {
        if (e.Action == Acms.DataControls.UpdateAction.Insert)
        {
            e.Item.SiteId = AcmsSite.Id;
        }
    }

    protected void dsTopics_BeforeUpdating(object sender, Acms.DataControls.BeforeUpdatingEventArgs e)
    {
        if(e.Values != null)
        {
            Trace.Warn("New Values");
            foreach (string key in e.Values.Keys)
            {
                if (e.Values[key] != null)
                {
                    Trace.Warn(key, e.Values[key].ToString());
                }
                else
                {
                    Trace.Warn(key, "null");
                }
            }
        }
        if (e.OldValues != null)
        {
            Trace.Warn("Old Values");
            foreach (string key in e.OldValues.Keys)
            {
                if (e.OldValues[key] != null)
                {
                    Trace.Warn(key, e.OldValues[key].ToString());
                }
                else
                {
                    Trace.Warn(key, "null");
                }
            }
        }
    }

    protected void dsAttachments_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.TopicFile> e)
    {
        int topicId = 0;
        if(gvTopics.SelectedDataKey != null && gvTopics.SelectedDataKey["Id"] is int)
        {
            topicId = (int)gvTopics.SelectedDataKey["Id"];
        }

        if (topicId > 0)
        {
            e.SetData(Acms.Bll.TopicFile.GetAllByTopicId(topicId));
        }
    }

    protected void gvTopics_SelectedIndexChanged(object sender, EventArgs e)
    {
        divAttachments.Visible = true;
        gvAttachments.DataBind();
    }

    protected void dsAttachments_UpdatingData(object sender, Acms.DataControls.UpdatingDataEventArgs<Acms.Bll.TopicFile> e)
    {
        if (e.Action == Acms.DataControls.UpdateAction.Insert)
        {
            //upload file and then set topic Id and file Id
            e.Item.TopicId = (int)gvTopics.SelectedDataKey["Id"];

            DropDownList fileTypeList = dvAttachments.FindControl("dlFileType") as DropDownList;
            FileUpload fileUpload = dvAttachments.FindControl("fuAttachment") as FileUpload;
            if(fileTypeList != null && fileUpload != null && fileUpload.HasFile)
            {
                Acms.Bll.File.FileTypes type = (Acms.Bll.File.FileTypes)Enum.Parse(typeof(Acms.Bll.File.FileTypes), fileTypeList.SelectedValue);
                Acms.Bll.File file = Acms.Bll.File.CreateInstance(type, fileUpload.PostedFile, fileUpload.FileBytes);
                file.Update();
                e.Item.FileId = file.Id;
            }
        }
    }
    protected void DropDownList1_DataBinding(object sender, EventArgs e)
    {
        DropDownList list = sender as DropDownList;
        list.DataSource = Enum.GetNames(typeof(Acms.Bll.File.FileTypes));
    }
}
